<template>
    <div class="card" @click.stop="handleClick">
        <div class="cover-container">
            <img :src="data.src" class="cover-image" :alt="data.title">
        </div>
        <div class="content">
            <h3 class="title">{{ data.title }}</h3>
            <div class="footer">
                <div class="author-info">
                    <img :src="avatarUrl" class="avatar" alt="author" @error="handleAvatarError">
                    <span class="name">{{ data.author }}</span>
                </div>
                <div class="stats">
                    <div class="stat-item">
                        <span class="icon">❤️</span>
                        {{ data.likes }}
                    </div>
                    <div class="stat-item">
                        <span class="icon">💬</span>
                        {{ data.comments }}
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { defineProps, defineEmits } from 'vue'
import { computed } from 'vue'
import defaultAvatar from '@/assets/images/default_avatar.png'  // 引入默认头像

interface CardData {
    id: number
    title: string
    src: string
    author: string
    avatar?: string
    likes: number
    comments: number
}

const props = defineProps<{
    data: CardData
}>()

const emit = defineEmits<{
    (e: 'card-click', id: number): void
}>()

const handleClick = () => {
    emit('card-click', props.data.id)
}

// 计算头像URL
const avatarUrl = computed(() => {
  return props.data.avatar || defaultAvatar
})

// 头像错误处理
const handleAvatarError = (e: Event) => {
    const target = e.target as HTMLImageElement
    if (target.src !== defaultAvatar) {
        target.src = defaultAvatar
    }
}
</script>

<style lang="less" scoped>
.card {
    background: #fff;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    transition: transform 0.3s ease, box-shadow 0.3s ease;
    cursor: pointer;

    &:hover {
        transform: translateY(-4px);
        box-shadow: 0 8px 20px rgba(0, 0, 0, 0.12);
    }

    .cover-container {
        position: relative;
        padding-top: 133.33%; // 3:4比例

        .cover-image {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            object-fit: cover;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
        }
    }

    .content {
        padding: 16px;

        .title {
            font-size: 15px;
            line-height: 1.4;
            margin: 0 0 12px;
            color: #333;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 2;
            line-clamp: 2; // 添加标准属性
            overflow: hidden;
            font-weight: 500;
        }

        .footer {
            display: flex;
            justify-content: space-between;
            align-items: center;

            .author-info {
                display: flex;
                align-items: center;
                flex: 1;
                min-width: 0;

                .avatar {
                    width: 28px;
                    height: 28px;
                    border-radius: 50%;
                    margin-right: 8px;
                    flex-shrink: 0;
                    object-fit: cover;
                    border: 2px solid #fff;
                    background: #f0f0f0;
                }

                .name {
                    font-size: 13px;
                    color: #666;
                    white-space: nowrap;
                    overflow: hidden;
                    text-overflow: ellipsis;
                }
            }

            .stats {
                display: flex;
                gap: 16px;
                font-size: 13px;
                color: #666;

                .stat-item {
                    display: flex;
                    align-items: center;
                    gap: 6px;
                }
            }
        }
    }
}
</style>